package com.itheima.controller;

import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 排行榜
 */
@RestController
@RequestMapping("/ranking")
public class RankingController {
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/rankCheckItemAndCheckGroup")
    public Result rankCheckItemAndCheckGroup(String date){
        try {
//            Set<String> ranking = jedisPool.getResource().zrange(date, 0, -1);
            Set<String> ranking = jedisPool.getResource().zrevrange(date, 0, -1);
            Set<Map> set=new HashSet<>();
            for (String s : ranking) {
                Double count = jedisPool.getResource().zscore(date, s);
                String chineseName = getChineseName(s);
                Map map=new HashMap();
                map.put("chineseName",chineseName);
                map.put("name",s);
                map.put("count",count);
                set.add(map);
            }
            return new Result(true, RedisConstant.SELECT_RANKING_SUCCESS,set);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,RedisConstant.SELECT_RANKING_FAIL);
        }

    }

    public String getChineseName(String name){
        if(name.equals("ADD_CHECKITEM")){
            return "增加检查项";
        }else if(name.equals("DELETE_CHECKITEM")){
            return "删除检查项";
        }
        else if(name.equals("UPDATE_CHECKITEM")){
            return "修改检查项";
        }
        else if(name.equals("SELECT_CHECKITEM")){
            return "查询检查项";
        }
        else if(name.equals("ADD_CHECKGROUP")){
            return "增加检查组";
        }
        else if(name.equals("DELETE_CHECKGROUP")){
            return "删除检查组";
        }
        else if(name.equals("UPDATE_CHECKGROUP")){
            return "修改检查组";
        }else{
            return "查询检查组";
        }
    }


}
